package state

func (this *luaState) Compare(idx1, idx2 int, op CompareOp) bool {
	a := this.stack.get(idx1)
	b := this.stack.get(idx2)
	switch op {
	case LUA_OPEQ:
		switch x := a.(type) {
		case nil:
			return b == nil
		case bool:
			y, ok := b.(bool)
			return ok && y == x
		case string:
			y, ok := b.(string)
			return ok && y == x
		case int64:
			switch y := b.(type) {
			case int64:
				return x == y
			case float64:
				return float64(x) == y
			default:
				return false
			}
		case float64:
			switch y := b.(type) {
			case int64:
				return float64(y) == x
			case float64:
				return x == y
			default:
				return false
			}
		default:
			return a == b
		}
	case LUA_OPLT:
		switch x := a.(type) {
		case string:
			y, ok := b.(string)
			return ok && x < y
		case int64:
			switch y := b.(type) {
			case int64:
				return x < y
			case float64:
				return float64(x) < y
			}
		case float64:
			switch y := b.(type) {
			case int64:
				return x < float64(y)
			case float64:
				return x < y
			}
		}
		panic("compare < error")
	case LUA_OPLE:
		switch x := a.(type) {
		case string:
			y, ok := b.(string)
			return ok && x <= y
		case int64:
			switch y := b.(type) {
			case int64:
				return x <= y
			case float64:
				return float64(x) <= y
			}
		case float64:
			switch y := b.(type) {
			case int64:
				return x <= float64(y)
			case float64:
				return x <= y
			}
		}
		panic("compare <= error")
	default:
		panic("unknow compare op")
	}
	return false
}
